package com.itheima.springbootwebtlias.controller;

import com.github.pagehelper.Page;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.pojo.StuQueryParam;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StuController {

    @Autowired
    private StuService stuService;

    // 学员列表查询
    @GetMapping
    public Result page(StuQueryParam queryParam){
        log.info("分页条件查询，请求参数：{}", queryParam);

        PageResult pageResult = stuService.page(queryParam);

        return Result.success(pageResult);
    }

    // 学员添加
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学员，请求参数：{}", student);

        stuService.add(student);

        return Result.success();
    }

    // 学员删除
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable List<Integer> ids){
        log.info("删除学员，请求参数：{}", ids);

        stuService.removeBatch(ids);

        return Result.success();
    }

    //根据id查询学员详情
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){

        Student student = stuService.queryById(id);

        return Result.success(student);
    }

    // 学员修改
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员，请求参数：{}", student);
        stuService.update(student);

        return Result.success();
    }

    // 违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("违纪处理，请求参数：{},{}", id, score);
        stuService.violation(id, score);

        return Result.success();
    }
}
